#include "sk_project_item.h"
#include "sk_level_item.h"
#include "sk_game_project.h"
#include "sk_game_level.h"
#include "sk_creation_params.h"
#include "userdata.h"

#include <QPainter>
#include <QDebug>

namespace sk
{
    // PROJECT ITEM

ProjectItem::ProjectItem(GameItem * /*parent*/, GameProject * project)
    :
GameItem(0)
{
    _project = project;
    setCanSwapChilds(true);
    setCanCloneChilds(true);
    _UD.pi = this;
}

ProjectItem::~ProjectItem()
{
    _UD.pi = 0;
}

GameItemType ProjectItem::type()
{
    return IT_Project;
}

int ProjectItem::numChilds()
{
    return _project->levels.size();
}

void ProjectItem::doDeleteChild(int ind)
{
    _project->removeLevel(ind);
}

void ProjectItem::doSwapChilds(int i, int j)
{
    _project->swapLevels(i, j);
}

GameItem * ProjectItem::createItem(void * params)
{
    sk::SimpleItemParam * p = static_cast<sk::SimpleItemParam *>(params);
    if(p->type == sk::IT_LEVEL)
    {
        QString * lvlName = static_cast<QString *>(p->param);
        _project->createLevel(*lvlName);
    }
    else
    {
        Q_ASSERT(p->type == sk::IT_CLONE);
        _project->cloneLevel(p->ind);
    }


    return child(numChilds() - 1);
}

GameItem * ProjectItem::getChildItem(int i)
{
    qDebug() << "GameItem * ProjectItem::getChildItem(int i)" << i;
    Q_ASSERT(i >= 0 && i < _project->levels.size());
    return new LevelItem(this, i);
}

void ProjectItem::paint(QPainter * painter, const QStyleOptionViewItem & option, int row, int column)
{
    QString n = _project->name();
    paintText(painter, option, &n, row, column);
}

} // namespace sk
